<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Document</title>
<link rel="stylesheet" href="./css/bootstrap.min.css">
<style>
*{
    padding: 0;
    margin: 0;
    list-style: none;
}
.kuang{
    width: 1200px;
    margin: 0 auto;
}
.left,.right{
    float:left;
    margin-right:30px;
}
.left{
    width: 400px;
}
.small ul li{
    float:left;
    width:50px;
    height:50px;
    border:1px solid #000;
    margin:10px;
}
.color span{
    margin-right:10px;
}
.right p {
    font-size: large;
}
#goodsname{
    color: red;
    font-size: 40px;
    font-weight: bolder;
}
#price{
    color:#f40;
    font-size: 40px;
    font-weight: bolder;
}
.shade{
    width: 100px;
    height: 100px;
    background:rgba(255,255,0,0.5);
    position: absolute;
    left:0;
    top: 0;
    display:none;
}
.big{
    width: 400px;
    height: 400px;
    position:absolute;
    left:105%;
    top:0;
    border:solid;
    background-size:1600px 1600px;
    display:none;
    background-repeat:no-repeat;
}
#header {
  height: 36px;
  border-bottom: 1px solid #cdcdcd;
  box-sizing: border-box;
  line-height: 35px;
  background-color: #f5f5f5;
}
#header .header_left {
  height: 35px;
  float: left;
}
#header .header_left li {
  float: left;
  height: 35px;
  margin: 0 6px;
}
#header .header_left li div {
  width: 79px;
  color: #919191;
}
#header .header_left li div span {
  font-size: 16px;
  margin-left: 7px;
  font-weight: bold;
  vertical-align: middle;
}
#header .header_left li div:hover {
  color: #ff8500;
}
#header .header_left li a {
  font-size: 12px;
  color: #919191;
}
#header .header_left li a:hover {
  color: #ff8500;
}
#header .header_right {
  height: 35px;
  float: right;
}
#header .header_right li {
  float: left;
  height: 35px;
  margin: 0 6px;
}
#header .header_right li span {
  font-size: 16px;
  margin-left: 7px;
  font-weight: bold;
  vertical-align: middle;
}
#header .header_right li i {
  font-size: 16px;
  margin-right: 5px;
  vertical-align: middle;
}
#header .header_right li a {
  font-size: 12px;
  color: #919191;
  vertical-align: middle;
}
#header .header_right li:hover {
  color: #ff8500;
}
#header .header_right li:hover a {
  color: #ff8500;
}
</style>
</head>
<body>
<div class="kuang">
    <div id="header">
        <div class="cantainer">
            <ul class="header_left">
                <li>
                   <div>
                       中国大陆<span class="iconfont icon-icon"></span>
                   </div>
                </li>
                <li>
                    <a href="#">亲，请登录</a>
                </li>
                <li>
                    <a href="#">免费注册</a>
                </li>
                <li>
                    <a href="#">手机逛淘宝</a>
                </li>
            </ul>
            <ul class="header_right">
                <li>
                   <div>
                       <a href="#">我的淘宝</a><span class="iconfont icon-icon"></span>
                   </div>
                </li>
                <li>
                    <i class="iconfont icon-gouwuche-copy"></i><a href="#">购物车</a><span class="iconfont icon-icon"></span>
                </li>
                <li>
                    <div>
                        <i class="iconfont icon-start"></i><a href="#">收藏夹</a><span class="iconfont icon-icon"></span>
                    </div>
                </li>
                <li>
                    <a href="#">商品分类</a>
                </li>
                <li>
                    <div>
                        <a href="#">千牛卖家中心</a><span class="iconfont icon-icon"></span>
                    </div>
                </li>
                <li>
                    <div>
                        <a href="#">联系客服</a><span class="iconfont icon-icon"></span>
                    </div>
                </li>
                <li>
                    <div>
                        <i class="iconfont icon-toggle"></i><a href="#">网站导航</a><span class="iconfont icon-icon"></span>
                    </div>
                </li>
            </ul>
        </div>    
    </div>

<div class="page-header">
    <h1>详情页面 <small>商品信息</small></h1>
</div>
<div class="header">
    <div class="left">
        <div class="middle">
            <img width="400" height="400" src="" id="middleImg">
            <div class="shade"></div>
            <div class="big"></div>
        </div>
        <div class="small">
            <ul>
            </ul>
        </div>
    </div>
    <div class="right">
        <p><b>商品名称：</b><span id="goodsname"></span></p>
        <p><b>商品价格：</b><span id="price">00</span>元</p>
        <p class="color"><b>款式：</b>
        </p>
        <p>
            <a href="#" class="btn btn-primary shopping" style="background-color:#f40;border-color:red;font-size:16px;margin-top: 180px;">加入购物车</a>
            <a href="shopping.html" class="btn btn-primary" style="background-color:#f40;border-color:red;font-size:16px;margin-top: 180px;">立即购买</a>
        </p>
    </div>
</div>
<div style="clear:both;"></div>
<div>
    <h1>商品描述：</h1>
    <div class="introduce">
    </div>
</div>
</div>
</body>
<script src="./js/promiseAjax.js"></script>
<script src="./js/jquery-3.4.1 .js"></script>
<script src="./js/cookie.js"></script>
<script type="text/javascript">
var params = window.location.search;
var reg = /id=(\d+)/;
var id = params.match(reg)[1];
sendAjax({
    url:"./server/detail.php",
    data:{
        id:id
    },
    type:"post",
    success:function(res){
        // console.log(res);
        if(res.status==200){
            // 成功之后加载数据
            var data = res.data;
            // console.log(data);
            $("#middleImg").attr("src",data.imgpath);
            $("#goodsname").text(data.name);
            $("#price").text('￥'+data.price);
            $(".introduce").html(data.introduce);
            // $("#big").attr("src",data.imgpath);
            // 很多小图使用 ==== 分割
            var  arr = data.smallimgs.split("====");
            // console.log(arr);
            for(var i=0;i<arr.length;i++){
                var li = $("<li><img src='"+arr[i]+"' width='50' height='50'></li>");
                $(".small ul").append(li);
            }
            // 款式           
            var styles = data.styles.split("====");
            for(var i=0;i<styles.length;i++){
                var span = $("<span class='btn btn-warning'>"+styles[i]+"</span>");
                
                $(".color").append(span);
            }
        }
    }
});
$(".shopping").click(function(){ 
    var username = getCookie("username");
    if(!username){ // 没有登陆
        alert("请先登陆");
        location.href = 'login.html'; 
        return false;
    }
    var data = localStorage.getItem("data");
    if(data){
        data = JSON.parse(data);
        var index = data.findIndex(function(v){
            return v.id == id;
        }); 
        if(index>=0){
            data[index].number++;
        }else{
            var obj = {
                username:username,
                id:id,
                number:1
            };
            data.push(obj);
        }
        localStorage.setItem("data",JSON.stringify(data));      
    }else{  
        var data = []; 
        var obj = { 
            username:username,
            id:id,
            number:1
        };
        data.push(obj);
        localStorage.setItem("data",JSON.stringify(data));
    }
    alert("商品添加成功！")
    return false;
});


</script>
</html>